package com.bayern.entity;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * LFU缓存对象
 * @author linling
 *
 */
public class LFUObject<K, V> extends CacheObject<K, V>
{

	//数据添加时间
	private long addTime;
	
	private AtomicInteger userCount;
	
	public LFUObject(Entry<K, V> entry)
	{
		this(entry, (new Date()).getTime());
	}
	
	public LFUObject(Entry<K, V> entry, long addTime)
	{
		super(entry);
		
		//数据使用次数, 创建时候默认为1次
		userCount = new AtomicInteger(1);
		this.addTime = addTime;
	}

	public long getAddTime()
	{
		return addTime;
	}

	public void setAddTime(long addTime)
	{
		this.addTime = addTime;
	}

	public AtomicInteger getUserCount() {
		return userCount;
	}

	public void setUserCount(AtomicInteger userCount) {
		this.userCount = userCount;
	}
	
	public int addUserCount()
	{
		return userCount.incrementAndGet();
	}

	@Override
	public int compareTo(CacheObject<K, V> o)
	{
		LFUObject<K, V> other = (LFUObject<K, V>)o;
		//如果两个元素使用次数一样，则先加入队列的先被移除
		if(userCount.get() == other.getUserCount().get())
		{
			return addTime > other.getAddTime() ? 1 : -1;
		}
		else
		{
			return userCount.get() - other.getUserCount().get();
		}
	}

}
